package com.example.hucanhui.keyboard;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.InputType;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.PopupWindow;

import java.lang.reflect.Method;

/**
 * Created by hucanhui on 2015/10/9.
 */
public class KeyBoardPopup implements View.OnClickListener{

    private EditText et_input;
    private PopupWindow keyBoard;
    private View contentView;
    private View parent;
    private int etX;
    private int etY;
    private Activity activity;
    private int windowHeight;
    private int keyboardHeight;

    public KeyBoardPopup(Activity activity, View parent, EditText et_input) {
        this.activity = activity;
        this.et_input = et_input;
        this.parent = parent;
        //sdgsdfgjds
        disableShowSoftInput(et_input);
        //TODO
        windowHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
    }


    public void setKeyBoardView(Context context, float keyboardHeightDp, int layout, int[] ids){
        keyboardHeight = PxUtils.Dp2Px(context, keyboardHeightDp);
        contentView = View.inflate(context, layout, null);

        for (int id : ids) {
            contentView.findViewById(id).setOnClickListener(this);
        }
    }

    /**
     * չʾ����
     */
    public void showKeyBoard(){

        keyBoard = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        keyBoard.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        keyBoard.showAtLocation(parent, Gravity.BOTTOM, 0, 0);

        int[] localtion = new int[2];
        et_input.getLocationOnScreen(localtion);
        etY = localtion[1];
        int offset = 0;

        if((etY + et_input.getHeight()) > (windowHeight - keyboardHeight)){
            offset = (etY + et_input.getHeight()) - (windowHeight - keyboardHeight) + 10;
            System.out.println("����");
            parent.setPadding(0, -offset, 0, 0);
        }
    }


    /**
     * �жϼ����Ƿ�����չʾ��false û��ʾ�� true ������ʾ��
     * @return
     */
    public boolean isShowing(){
        if(keyBoard == null){
            return false;
        }else {
            return keyBoard.isShowing();
        }
    }


    /**
     * �������̰����Ľӿ�
     * �����̰���������ʱ����onKeyDown������ͨ��v.getId�жϰ��µ����ĸ�����������Ӧ����
     */
    public interface OnKeyDownListener{
        void onKeyDown(View v);
    }


    private OnKeyDownListener listener;

    /**
     *���ô˷��������������ü���
     * @param listener
     */
    public void setOnKeyDownListener(OnKeyDownListener listener){
       this.listener = listener;
    }

    /**
     * ���̱����ʱ�������µĽ���
     * @param v
     */
    @Override
    public void onClick(View v) {
        //�����ѵ����ż���
        if(keyBoard.isShowing()) {
            listener.onKeyDown(v);
        }
    }


    /**
     * �������׷������
     * @param str Ҫ׷�ӵ�����
     */
    public void append2EditText(String str){
        et_input.setText(et_input.getText().toString() + str);
        et_input.setSelection(et_input.getText().toString().length());
    }

    /**
     * ��������������
     * @param str ����
     */
    public void setTextForEditText(String str){
        et_input.setText(str);
        et_input.setSelection(et_input.getText().toString().length());
    }

    /**
     * ɾ�����������
     */
    public void delete(){
        if (et_input.getText().toString().length() > 1) {
            String input = et_input.getText().toString();
            et_input.setText(input.substring(0, input.length() - 1));
        } else {
            et_input.setText("");
        }
        et_input.setSelection(et_input.getText().toString().length());
    }

    /**
     * ���ؼ���
     */
    public void dismiss(){
        if(keyBoard != null && keyBoard.isShowing()){
            parent.setPadding(0, 0, 0, 0);
            keyBoard.dismiss();
            keyBoard = null;
        }
    }


    /**
     * ������activity��dispatchTouchEvent�����е��ô˷�����
     * ���������ڼ����⣬���ĳ������ʱ�����ؼ��̡�
     * @param  ev MotionEvent
     * @param  v View  ��ָ��view��
     */
    public void setYouNeed2InterceptView(MotionEvent ev, View v){
        int[] location = new int[2];
        v.getLocationOnScreen(location);
        int x = location[0];
        int y = location[1];
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                if(ev.getRawX() < x || ev.getRawX() > (x + v.getWidth())
                        || ev.getRawY() < y || ev.getRawY() > (y + v.getHeight())
                        && isShowing()){
                    dismiss();
                }
        }
    }


    /**
     * ��ֹEdittext��������̣������Ȼ������ʾ��
     */
    public void disableShowSoftInput(EditText et_input)
    {
        if (android.os.Build.VERSION.SDK_INT <= 10)
        {
            et_input.setInputType(InputType.TYPE_NULL);
        }
        else {
            Class<EditText> cls = EditText.class;
            Method method;
            try {
                method = cls.getMethod("setShowSoftInputOnFocus",boolean.class);
                method.setAccessible(true);
                method.invoke(et_input, false);
            }catch (Exception e) {
                // TODO: handle exception
            }

            try {
                method = cls.getMethod("setSoftInputShownOnFocus",boolean.class);
                method.setAccessible(true);
                method.invoke(et_input, false);
            }catch (Exception e) {
                // TODO: handle exception
            }
        }
    }
}
